package com.example.zyfyptlzx729.activities;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.view.menu.ActionMenuItemView;

import com.example.zyfyptlzx729.R;
import com.example.zyfyptlzx729.common.Common;
import com.example.zyfyptlzx729.iface.CollectListener;
import com.example.zyfyptlzx729.iface.FocusListener;
import com.example.zyfyptlzx729.model.CollectModel;
import com.example.zyfyptlzx729.model.FocusModel;

public class ViewtcaseActivity extends AppCompatActivity {
    private WebView webView;
    private int tcase, authorid;//资源id
    Context context;
//    private int tcase;

    private Boolean flagcollect = false;//收藏标志
    private boolean flagfocus =false;
    private CollectModel collectmodel;//收藏model
    private FocusModel focusmodel;
    private SharedPreferences sp;//简单存储
    private String sessionID = "";  //sessionid
    private ActionMenuItemView item,item2;

    CollectListener collectListener = new CollectListener() {
        @SuppressLint("RestrictedApi")
        @Override
        public void onResponse(String msg) {
            //获取菜单视图
            if (item == null)
                item = findViewById(R.id.menucollect);//注意菜单id
            //根据mode中response返回的字符串区分返回结果
            switch (msg) {
                case "1":
                    System.out.println("----收藏失败");
                    break;
                case "2":
                    System.out.println("----收藏成功");
                    flagcollect = true;
                    item.setTitle("取消收藏");
                    break;
                case "3":
                    System.out.println("----取消收藏失败");
                    break;
                case "4":
                    System.out.println("----取消收藏成功");
                    flagcollect = false;
                    item.setTitle("收藏");
                    break;
                case "5":
                    System.out.println("----已收藏");
                    flagcollect = true;
                    item.setTitle("取消收藏");
                    break;
                case "6":
                    System.out.println("----未收藏");
                    flagcollect = false;
                    item.setTitle("收藏");
                    break;
                default:
                    Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
        }


    };
    FocusListener focusListener = new FocusListener() {
        @SuppressLint("RestrictedApi")
        @Override
        public void onResponse(String msg) {
            //获取菜单视图
            if (item2 == null)
                item2 = findViewById(R.id.menufocus);//注意菜单id
            //根据mode中response返回的字符串区分返回结果
            switch (msg) {
                case "1":
                    System.out.println("----关注失败");
                    break;
                case "2":
                    System.out.println("----关注成功");
                    flagfocus = true;
                    item2.setTitle("取消关注");
                    break;
                case "3":
                    System.out.println("----取消关注失败");
                    break;
                case "4":
                    System.out.println("----取消关注成功");
                    flagfocus = false;
                    item2.setTitle("关注");
                    break;
                case "5":
                    System.out.println("----已关注");
                    flagfocus = true;
                    item2.setTitle("取消关注");
                    break;
                case "6":
                    System.out.println("----未关注");
                    flagfocus = false;
                    item2.setTitle("关注");
                    break;
                default:
                    Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_article);
        context = ViewtcaseActivity.this;
        initViews();
        tcase = getIntent().getIntExtra("resid", 1);//获取传递的资源id
        authorid = getIntent().getIntExtra("authorid", 1);//获取传递的资源id


        sp = context.getSharedPreferences("login", MODE_PRIVATE);//注意文件名


        readSP();//读取sessionid
    }

    private void initViews() {
        webView = findViewById(R.id.webview);
        tcase = getIntent().getIntExtra("resid", -1);

//        tcase = getIntent().getIntExtra("tcase", -1);

        authorid = getIntent().getIntExtra("authorid", -1);
        webView.loadUrl(Common.TCASEURL + tcase);
    }

    private void readSP() {
        sessionID = sp.getString("session", null);//注意名值对的名
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menucollect, menu);//加载菜单布局
        collectmodel = new CollectModel();//实例化对象
        focusmodel = new FocusModel();
        collectmodel.exist("tcase", tcase, sessionID, collectListener);//判断是否收藏
        focusmodel.exist("userfocus", authorid, sessionID, focusListener);//判断是否关注
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menucollect:
                if (flagcollect)//如果已收藏，则调用取消收藏
                {
                    System.out.println("----准备取消收藏");
                    collectmodel.uncollect("tcase", tcase, sessionID, collectListener);
                } else//如果未收藏，则调用收藏
                {
                    System.out.println("----准备收藏");
                    collectmodel.collect("tcase", tcase, sessionID, collectListener);
                }
                break;
            case R.id.menufocus:
                if (flagfocus)//如果已关注，则调用取消关注
                {
                    System.out.println("----准备取消关注");
                    focusmodel.unfocus("userfocus", authorid, sessionID, focusListener);
                } else//如果未关注，则调用关注
                {
                    System.out.println("----准备关注");
                    focusmodel.focus("userfocus", authorid, sessionID, focusListener);
                }
                break;
        }
        return super.onOptionsItemSelected(item);
    }

}
